//
//  LTLExpression.m
//  WeiBo
//
//  Created by Apple_Lzzy46 on 16/11/10.
//  Copyright © 2016年 LTL. All rights reserved.
//

#import "LTLExpression.h"
#import "LTLKeyboardFayout.h"
#import "LTLEmoticonCell.h"
#import "LTLEmoticonManager.h"

@interface LTLExpression () <UICollectionViewDataSource, UICollectionViewDelegate>
//UICollectionView
@property (nonatomic,strong) UICollectionView *collectionView;
//指示器
@property (nonatomic,strong) UIPageControl *pageControl;
//表情管理
@property (nonatomic,strong) LTLEmoticonManager *Manager;

@end
//重用 ID
static NSString *ID = @"EmoticonCell";

@implementation LTLExpression
#pragma mark - 初始化
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        ///添加控件
        [self prepareUI];
   
    }
    return self;
}
///返回组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return [LTLEmoticonManager sharedManager].packages.count;
}
//返回每组行数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [[LTLEmoticonManager sharedManager] numberOfPagesInSection:section];
//    return 2;
}
// cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LTLEmoticonCell *cell = [collectionView
                            dequeueReusableCellWithReuseIdentifier:ID
                            forIndexPath:indexPath];
    
    /// 给数据模型
    cell.arryr = [[LTLEmoticonManager sharedManager] emoticonsWithIndexPath:indexPath];
    
    
    return cell;
}
#pragma mark - UIScrollView Delegate
///给_pageControl赋值移动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    ///中心点
    CGPoint center = scrollView.center;
    center.x += scrollView.contentOffset.x;
    //获取可见cell 的下标
    NSArray *indexPaths = [_collectionView indexPathsForVisibleItems];
    
    NSIndexPath *targetPath = nil;
    for (NSIndexPath *indexPath in indexPaths)
    {
        UICollectionViewCell *cell = [_collectionView cellForItemAtIndexPath:indexPath];
        ///判断中心是否在cell 范围内
        if (CGRectContainsPoint(cell.frame, center))
        {
            ///取下标
            targetPath = indexPath;
            break;
        }
    }
    
    if (targetPath != nil)
    {
        ///更新指示控制器
        [self updatePageControlWithIndexPath:targetPath];
        
    }
}
- (void)updatePageControlWithIndexPath:(NSIndexPath *)indexPath {
    ///总页数
    _pageControl.numberOfPages = [[LTLEmoticonManager sharedManager] numberOfPagesInSection:indexPath.section];
    ///当前页
    _pageControl.currentPage = indexPath.item;
    
    if ([self.delegate respondsToSelector:@selector(LTLExpressionSection:)]) {
        [self.delegate LTLExpressionSection:indexPath.section];
    }
    
}
//设置控件
- (void)prepareUI {
 

    // 1. 添加 collectionView
    CGRect collectionViewRect = self.frame;
    self.collectionView = [[UICollectionView alloc]
                       initWithFrame:collectionViewRect
                       collectionViewLayout:[[LTLKeyboardFayout alloc] init]];
    [self addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor clearColor];
    // 设置 collectionView
    
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    [_collectionView registerClass:[LTLEmoticonCell class] forCellWithReuseIdentifier:ID];
    
    // 2. 分页控件
    self.pageControl = [[UIPageControl alloc] init];
    [self addSubview:_pageControl];
    _pageControl.backgroundColor = [UIColor clearColor];
    // 设置分页控件
    _pageControl.hidesForSinglePage = YES;
    _pageControl.userInteractionEnabled = NO;
    
    [_pageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_selected"] forKey:@"_currentPageImage"];
    [_pageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_normal"] forKey:@"_pageImage"];
    
}
///布局
-(void)layoutSubviews
{
    [super layoutSubviews];
    //自动布局
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
    }];
    [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.mas_equalTo(36);
    }];
    //设置默认
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:1];
    
    [_collectionView scrollToItemAtIndexPath:indexPath
                            atScrollPosition:UICollectionViewScrollPositionLeft
                                    animated:NO];
    [self updatePageControlWithIndexPath:indexPath];
}


//设置表情类型
-(void)setSection:(NSInteger)section
{
    _section = section;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:section];
    
    [_collectionView scrollToItemAtIndexPath:indexPath
                            atScrollPosition:UICollectionViewScrollPositionLeft
                                    animated:NO];
    [self updatePageControlWithIndexPath:indexPath];

}


@end
